package com.example.bills;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.bills.MultiChoiceDialog.OnSelectedVisibleListener;
import com.example.bills.YesOrNoDialog.OnSelecteChoiceListener;
import com.example.finalapp.R;
import com.parse.ParseUser;

public class Bill_InformationActivity extends Activity implements OnSelecteChoiceListener,OnSelectedVisibleListener
{
	boolean resumedFromUrl = false;
	MultiChoiceDialog newDialog;
	RadioButton paidBtn ;
	EditText dueDate;
	EditText createdBy;
	EditText paidBy;
	EditText amountPerRoomie;
	EditText editAmount;
	Intent intent;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill__information);
		
		intent 				   = getIntent();
		paidBtn 			   = (RadioButton)findViewById(R.id.statusPaid);
		RadioGroup statusGroup = (RadioGroup) findViewById(R.id.billStatusRadioGroup);
		dueDate				   = (EditText)findViewById(R.id.editDueDate);
		createdBy              = (EditText)findViewById(R.id.edit_create);
		paidBy				   = (EditText)findViewById(R.id.edit_paidByView);
		amountPerRoomie		   = (EditText)findViewById(R.id.amountPerRoomie);
		editAmount			   = (EditText)findViewById(R.id.editAmount);
		
		newDialog = new MultiChoiceDialog();
		
		createdBy.setText(ParseUser.getCurrentUser().get("Name").toString());
		paidBy.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) 
			{
				Intent intent = getIntent();
				Bundle args   = new Bundle();
				CharSequence roommies[] = intent.getCharSequenceArrayExtra("Roommates");
				boolean mCheckedItemsArray[] = new boolean[roommies.length];
				args.putCharSequenceArray(MultiChoiceDialog.ARG_STRING_ID,roommies );
				args.putBooleanArray(MultiChoiceDialog.Bool_Array_ID, mCheckedItemsArray);//{false,false,false} instead of mCheckedArray
				
				newDialog.setArguments(args);
				newDialog.show(getFragmentManager(), "newDialog");
			}
		});
		editAmount.addTextChangedListener(new TextWatcher()
		{
	        public void afterTextChanged(Editable s) 
	        {
	        	int devidor 	 = intent.getIntExtra("visibleToSize", 0);
	        	int amount;
	        	String amountStr = editAmount.getText().toString();
	        	
	        	if( amountStr.matches(""))
	        	{
	        		amount	 = 0;
	        	}
	        	else
	        	{
	        		amount	 = Integer.parseInt(amountStr);
	        	}
	        	
	        	double perRoomie = (double)amount/ (double)devidor;
	        	
	        	if(perRoomie == 0)
	        	{
	        		amountPerRoomie.setText("");
	        	}
	        	else
	        	{
	        		amountPerRoomie.setText(String.valueOf(perRoomie));
	        	}
	        	
	        }
	        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
	        public void onTextChanged(CharSequence s, int start, int before, int count){}
	    }); 
		statusGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
		{
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{

				if (checkedId == R.id.statusPaid) 
				{
					DisableEnableAllViews(false);
				} 
				else
				{
					if (checkedId == R.id.statusUnpaid) 
					{
						DisableEnableAllViews(true);
					}
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.bill__information, menu);
		return true;
	}

	@Override
	protected void onResume() 
	{
		super.onResume();
		YesOrNoDialog new_dialog= new YesOrNoDialog();

		if(resumedFromUrl == true)/*pop up a dialog asking if bill was paid from url*/
		{
			Bundle args = new Bundle();
			args.putString(YesOrNoDialog.ARG_STRING_ID, getResources().getString(R.string.paid_from_url));
			new_dialog.setArguments(args);
			new_dialog.show(getFragmentManager(), "newAdminDialog");
		}
	}

	@Override
	public void onYesSelected(boolean isAdminDialog) 
	{	/*Bill was indeed paid by URL*/

		paidBtn.setChecked(true);
		DisableEnableAllViews(false);
	}

	@Override
	public void onNoSelected(boolean isAdminDialog)
	{	/*Bill was not paid by URL*/

	}

	public void PayBtn(View v)
	{
		
		String url = intent.getExtras().getString("payment");

		if(!url.matches(""))
		{
			Uri uriUrl = Uri.parse("http://"+url);
			Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
			resumedFromUrl = true;
			startActivity(launchBrowser);
		}
		else
		{
			paidBtn.setChecked(true);
		}
	}

	public void DisableEnableAllViews(boolean aDisableEnableFlag)
	{
		int size;
		int innerSize;

		LinearLayout myLayout = (LinearLayout) findViewById(R.id.Bill_Information_Layout);
		size = myLayout.getChildCount();

		for ( int outerIndex = 0; outerIndex < size;  outerIndex++ )
		{
			View view = myLayout.getChildAt(outerIndex);
			innerSize = ((ViewGroup)view).getChildCount();

			for(int innerIndex = 0 ; innerIndex < innerSize ; innerIndex++)
			{
				View InnerView = ((ViewGroup)view).getChildAt(innerIndex);
				InnerView.setEnabled(aDisableEnableFlag); // Or whatever you want to do with the view.
			}

		}
	}
	@Override
	public void onCancelSelected(boolean isAdminDialog) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConfirmSelected(ArrayList<String> aStringArray,boolean[] checkedItems) 
	{
		int aStringArrayIndex; 
		int sizeOfaStringArray;

		sizeOfaStringArray = aStringArray.size();
		StringBuilder tempString = new StringBuilder();

		for(aStringArrayIndex = 0; aStringArrayIndex < sizeOfaStringArray;aStringArrayIndex++)
		{

			if(aStringArrayIndex == sizeOfaStringArray-1)
			{
				tempString.append(aStringArray.get(aStringArrayIndex));
			}
			else
			{
				tempString.append(aStringArray.get(aStringArrayIndex)+",");
			}
			if(sizeOfaStringArray == 0)
			{
				paidBy.setText("");
			}

			paidBy.setText(tempString.toString());
		}

	}

}
